package dto

import (
	"appshop/app/admin/models"
	"mime/multipart"
)

// AppReq 新增应用
type AppReq struct {
	PackageBundleId string                `form:"packageBundleId" json:"packageBundleId" binding:"required"`
	PackageDesc     string                `form:"packageDesc" json:"packageDesc"`
	PackageName     string                `form:"packageName" json:"packageName" binding:"required"`
	PackageVersion  string                `form:"packageVersion" json:"packageVersion" binding:"required"`
	PackageFile     *multipart.FileHeader `form:"packageFile" json:"packageFile" binding:"required"`
	PackageIcon     *multipart.FileHeader `form:"packageIcon" json:"packageIcon" binding:"omitempty"`
}

// AppDetails 应用明细
type AppDetails struct {
	Id                int64  `json:"id"`
	PackageBundleId   string `json:"packageBundleId"`
	PackageDesc       string `json:"packageDesc"`
	PackageName       string `json:"packageName"`
	PackagePath       string `json:"packagePath"`
	PackageVersion    string `json:"packageVersion"`
	PackageIcon       string `json:"packageIcon"`
	PackageAddTime    string `json:"packageAddTime"`
	PackageUpdateTime string `json:"packageUpdateTime"`
}

type AppCollection struct {
	CurrentApp AppDetails   // 当前最新的app版本
	History    []AppDetails // 历史app列表，按照版本倒排
}

// AppSearch 查询用dto，面向表现层
type AppSearch struct {
	PackageBundleId string `db:"bundle_id"`
	PackageName     string `db:"name"`
	PackagePath     string `db:"path"`
	PackageVersion  string `db:"version"`
	PackageAddTime  string `db:"add_time"`
}

// Generate 将AppReq 转换为 models.App
func (a AppReq) Generate() models.App {
	var app models.App
	app.PackageBundleId = a.PackageBundleId
	app.PackageDesc = a.PackageDesc
	app.PackageName = a.PackageName
	app.PackageVersion = a.PackageVersion
	return app
}

// Generate 将models.App 转换为 AppDetails
func Generate(m models.App) AppDetails {
	var ad AppDetails
	ad.Id = m.Id
	ad.PackageBundleId = m.PackageBundleId
	ad.PackageDesc = m.PackageDesc
	ad.PackageName = m.PackageName
	ad.PackagePath = m.PackagePath
	ad.PackageVersion = m.PackageVersion
	ad.PackageIcon = m.PackageIcon
	ad.PackageAddTime = m.PackageAddTime.Format("2006-01-02 15:04:05")
	ad.PackageUpdateTime = m.PackageUpdateTime.Format("2006-01-02 15:04:05")
	return ad
}
